package com.education.basic.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.education.basic.ao.DepartmentAo;
import com.education.basic.entity.Department;
import com.yunshangwoniu.core.baseAction.BaseAction;
import com.yunshangwoniu.core.page.Page;

@Controller
@Scope("prototype")
@Namespace("/department")
@ParentPackage(value="struts-default")
public class DepartmentAction extends BaseAction
{
	private static final long serialVersionUID = 1L;
	@Resource
	private DepartmentAo departmentAo;		//Ao层
	private Integer pageNo;					//当前页
	private Page<Department> page;			//分页装载器
	private Department depertment;			//查询的department
	private long id;						//删除或者查询的id
	private String name ="";				//搜索的院系名
	private String ids ;					//批量删除的id
	
	
	
	@Action(value="list",results={@Result(name="success",location="list.jsp")})
	public String list()
	{

        System.out.println("hello hot ~!");
		pageNo = pageNo==null?1:pageNo;
		
		Map<String,Object> params = new HashMap<String,Object>();
		page = departmentAo.departmentList(pageNo, 10, params,getRequest());
		
		return SUCCESS;
	}
	
	@Action(value="add",results={@Result(name="success",location="add.jsp")})
	public String add()
	{
		return SUCCESS;
	}
	
	@Action(value="save",results={@Result(name="success",location="../inc/message.jsp")})
	public String save()
	{
		Department d = departmentAo.newDepartment(depertment);
		setCallback("/department/list.htm");
		return d!=null?SUCCESS:ERROR;
	}
	
	@Action(value="delete")
	public void delete() throws IOException
	{
		departmentAo.removeDepartment(id);
		PrintWriter pw = getResponse().getWriter();
		pw.print("success");
		pw.flush();
		pw.close();
	}
	
	@Action(value="edit",results={@Result(name="success",location="edit.jsp")})
	public String edit()
	{
		depertment = departmentAo.findDepartmentById(id);
		
		return depertment==null?ERROR:SUCCESS;
	}
	
	@Action(value="update",results={@Result(name="success",location="../inc/message.jsp")})
	public String update()
	{
		departmentAo.update(depertment);
		setCallback("/department/list.htm");
		
		return SUCCESS;
	}
	
	@Action(value="search",results={@Result(name="success",location="list.jsp")})
	public String search()
	{
		name= name ==null||name==""?name:name.trim();
		page = departmentAo.findDepartmentByName(name,getRequest());
		
		return SUCCESS;
	}
	
	@Action(value="batchDelete")
	public void batchDelete() throws IOException
	{
		String[] arr = ids.split("\\,");
		List<Long> ids2 = new ArrayList<Long>();
		for(String str :arr)
		{
			ids2.add(Long.parseLong(str));
		}
		departmentAo.batchDeleteDepartment(ids2);
		PrintWriter pw = getResponse().getWriter();
		pw.print("success");
		pw.flush();
		pw.close();
	}
	
	
	public DepartmentAo getDepartmentAo()
	{
		return departmentAo;
	}

	public void setDepartmentAo(DepartmentAo departmentAo)
	{
		this.departmentAo = departmentAo;
	}

	public Integer getPageNo()
	{
		return pageNo;
	}

	public void setPageNo(Integer pageNo)
	{
		this.pageNo = pageNo;
	}

	public Page<Department> getPage()
	{
		return page;
	}

	public void setPage(Page<Department> page)
	{
		this.page = page;
	}

	public static long getSerialversionuid()
	{
		return serialVersionUID;
	}

	public Department getDepertment()
	{
		return depertment;
	}

	public void setDepertment(Department depertment)
	{
		this.depertment = depertment;
	}

	public long getId()
	{
		return id;
	}

	public void setId(long id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getIds()
	{
		return ids;
	}

	public void setIds(String ids)
	{
		this.ids = ids;
	}
	
}
